//
//  UIImageView+FGChat.m
//  FGChatKit
//
//  Created by Henry on 2020/8/13.
//  Copyright © 2020 Henry. All rights reserved.
//

#import "UIImageView+FGChat.h"
#import <SDWebImage/SDWebImage.h>
#import "UIImage+FGUtil.h"
#import "UIImage+Addtions.h"
#import "UIImage+FGChat.h"
#import "FGChatHelper.h"
#import "FGResources.h"
#import "FGChatConfiguration.h"
#import "NSString+FGHash.h"
#import "FGPathTool.h"
#import "UIImageView+FGCornerRadius.h"

@implementation UIImageView (FGChat)

- (void)fg_setAvatarWithUrl:(NSString *)urlStr ID:(NSString *)ID name:(NSString *)name {
    
    UIImage *defaultHead = [FGResources getImageFromMessage:@"default_head"];
    
    if (!ID || ID.length == 0) {
        self.image = defaultHead;
        return;
    }
    
    NSString *avatarName = [ID fg_md5String];
    NSString *imagePath = [FGChatHelper pathAvatarImage:avatarName];
//    if ([FGPathTool isExistFile:imagePath]) { 原来
    if ([FGPathTool isExistFile:imagePath] && (!urlStr || urlStr.length == 0)) {
        self.image = [UIImage imageWithContentsOfFile:imagePath];
        return;
    }
    
    if (!urlStr || urlStr.length == 0) {
        UIImage *image = [UIImage getImage:name];
        NSData *imgData = UIImageJPEGRepresentation(image, 1);
        [[NSFileManager defaultManager] createFileAtPath:imagePath contents:imgData attributes:nil];
        self.image = image;
        return;
    }
    
    NSURL *url = [NSURL URLWithString:urlStr];

    [self sd_setImageWithURL:url placeholderImage:defaultHead options:SDWebImageDecodeFirstFrameOnly|SDWebImageAllowInvalidSSLCertificates completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
        if (error) {
            UIImage *image = [UIImage getImage:name];
            NSData *imgData = UIImageJPEGRepresentation(image, 1);
            [[NSFileManager defaultManager] createFileAtPath:imagePath contents:imgData attributes:nil];
            self.image = image;
        }
    }];
    
}

- (void)fg_setConvWithUrl:(NSString *)urlStr ID:(NSString *)ID name:(NSString *)name {
    UIImage *defaultHead = [FGResources getImageFromMessage:@"default_head"];

    if (!ID || ID.length == 0) {
        self.image = defaultHead;
        return;
    }
    
    NSString *avatarName = [ID fg_md5String];
    NSString *imagePath = [FGChatHelper pathAvatarImage:avatarName];
    if ([FGPathTool isExistFile:imagePath]) {
        self.image = [UIImage imageWithContentsOfFile:imagePath];
        return;
    }
    
    if (!urlStr || urlStr.length == 0) {
        
        NSArray *images = [name componentsSeparatedByString:@","];
        NSMutableArray *imageArr = [NSMutableArray array];
        for (NSString *name in images) {
            UIImage *image = [UIImage getImage:name];
            [imageArr addObject:image];
        }
        
        UIImage *gImage = [UIImage groupIconWith:imageArr bgColor:[UIColor lightGrayColor]];
        NSData *imgData = UIImageJPEGRepresentation(gImage, 1);
        [[NSFileManager defaultManager] createFileAtPath:imagePath contents:imgData attributes:nil];
        
        self.image = gImage;
        return;
    }
    
    NSURL *url = [NSURL URLWithString:urlStr];

    [self sd_setImageWithURL:url placeholderImage:defaultHead options:SDWebImageDecodeFirstFrameOnly|SDWebImageAllowInvalidSSLCertificates completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
        if (error) {
            
            NSArray *images = [name componentsSeparatedByString:@","];
            NSMutableArray *imageArr = [NSMutableArray array];
            for (NSString *name in images) {
                UIImage *image = [UIImage getImage:name];
                [imageArr addObject:image];
            }
            
            UIImage *gImage = [UIImage groupIconWith:imageArr bgColor:[UIColor lightGrayColor]];
            NSData *imgData = UIImageJPEGRepresentation(gImage, 1);
            [[NSFileManager defaultManager] createFileAtPath:imagePath contents:imgData attributes:nil];
            
            self.image = gImage;
            
        }
    }];
}

- (void)fg_setConvImage:(FGConversationModel *)model {
    if (model.iconImage) {
        self.image = model.iconImage;
        return;
    }
    UIImage *defaultHead = [FGResources getImageFromMessage:@"default_head"];
    if (model.identifier.length == 0) {
        self.image = defaultHead;
        return;
    }
    
    NSString *name = [model.identifier fg_md5String];
    NSString *path = [FGChatHelper pathConvHead:model.identifier name:name];
    if ([FGPathTool isExistFile:path]) {
        self.image = [UIImage imageWithContentsOfFile:path];
        return;
    }
    
    if (model.icon.length == 0) {
        self.image = [UIImage fg_getConversationImage:model];
        return;
    }
    
    NSURL *url = [NSURL URLWithString:model.icon];
    [self sd_setImageWithURL:url placeholderImage:defaultHead options:SDWebImageDecodeFirstFrameOnly|SDWebImageAllowInvalidSSLCertificates completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
        if (error) {
            self.image = [UIImage fg_getConversationImage:model];
        }
    }];
}


- (void)fg_setImageWithURL:(NSURL *)url {
    UIImage *defaultHead = [FGResources getImageFromMessage:@"default_head"];
    [self sd_setImageWithURL:url placeholderImage:defaultHead options:SDWebImageDecodeFirstFrameOnly|SDWebImageAllowInvalidSSLCertificates context:nil];
}

- (void)hc_setCornerRadius {
    [self fg_cornerRadius:[FGChatConfiguration shareInstance].defaultAvatarCorner];
}

@end

